package com.lzy.cloud.permission.controller;

import com.lzy.cloud.permission.dtos.UserRoleDto;
import com.lzy.cloud.permission.request.UserRoleRequest;
import com.lzy.cloud.permission.response.UserRoleResponse;
import com.lzy.cloud.permission.service.UserRoleService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by WIN7 on 2018/5/17.
 */
@RestController
@RequestMapping("/user/role")
public class UserRoleController {

    @Autowired
    UserRoleService userRoleService;

    /**
     * 新增用户角色关联
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add")
    public Result addUserRole(@RequestBody(required = false) UserRoleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = userRoleService.insert(DozerUtils.map(request, UserRoleDto.class));
        Ensure.that(ok).isTrue("00060001");
        return new Result(ok);
    }

    /**
     * 删除用户角色关联
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/del")
    public Result delRole(@RequestBody(required = false) UserRoleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = userRoleService.delete(DozerUtils.map(request, UserRoleDto.class));
        Ensure.that(ok).isTrue("00060002");
        return new Result(ok);
    }

    /**
     * 查询用户角色关联列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",  method = RequestMethod.POST)
    public Result selectList(UserRoleRequest request) {
        List<UserRoleDto> userRoleDtoList = userRoleService.selectList(DozerUtils.map(request, UserRoleDto.class));
        if (userRoleDtoList != null && userRoleDtoList.size() > 0) {
            return new Result(DozerUtils.maps(userRoleDtoList, UserRoleResponse.class));
        }
        return new Result();
    }

    /**
     * 根据userId查询用户角色关联列表
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/list/{userId}",  method = RequestMethod.GET)
    public Result selectList2(@PathVariable("userId") Long userId) {
        UserRoleDto userRoleDto = new UserRoleDto();
        userRoleDto.setUserId(userId);
        List<UserRoleDto> userRoleDtoList = userRoleService.selectList(userRoleDto);
        if (userRoleDtoList != null && userRoleDtoList.size() > 0) {
            return new Result(DozerUtils.maps(userRoleDtoList, UserRoleResponse.class));
        }
        return new Result();
    }

    /**
     * 查询单个用户角色
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/select/one")
    public Result selectUserRole(UserRoleRequest request) {
        Ensure.that(request).isNotNull("00010001");
        UserRoleDto userRoleDto = userRoleService.selectUserRole(DozerUtils.map(request, UserRoleDto.class));
        if (userRoleDto != null) {
            return new Result(DozerUtils.map(userRoleDto, UserRoleResponse.class));
        }
        return new Result();
    }
}
